#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Mvc;
using log4net;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Web.Controllers
{
    public class CategoryController : ApiController
    {
        private readonly CategoryService _categoryService;

        public CategoryController(CategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        public IEnumerable<Category> ListChildCategories(Category category)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var categories =
                    _categoryService.GetAll().Where(
                        c => (category.Id > 0 ? c.ParentCategoryId == category.Id : c.ParentCategory == null));
                return categories;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        //public JsonResult List(string search = "", int pageIndex = 0, int pageSize = 10)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var page = search != ""
        //                       ? _categoryService.GetAllPagedSearched(search, pageIndex, pageSize)
        //                       : _categoryService.GetAllPaged(pageIndex, pageSize);
        //        var categories = page.Entities.ToList().Select(e => new
        //                                                                {
        //                                                                    e.Id,
        //                                                                    e.Created,
        //                                                                    e.Updated,
        //                                                                    Description =
        //                                                                CategoryNew.ResourceManager.GetString(e.Key,
        //                                                                                                      Thread.
        //                                                                                                          CurrentThread
        //                                                                                                          .
        //                                                                                                          CurrentCulture),
        //                                                                    e.Key
        //                                                                });

        //        return
        //            Json(
        //                new
        //                    {
        //                        search,
        //                        page.Count,
        //                        page.CurrentPage,
        //                        page.PageSize,
        //                        page.PagesCount,
        //                        Entities = categories
        //                    }, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //[System.Web.Mvc.HttpPost]
        //public JsonResult Save(Category category)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        _categoryService.SaveOrUpdate(category);
        //        return Json(category.Id, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //[System.Web.Mvc.HttpPost]
        //public JsonResult DeleteCategory(int id)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var category = _categoryService.GetById(id);
        //        _categoryService.Delete(category);
        //        return Json("OK", JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //public JsonResult GetCategoriesAutocomplete(string searchTerm, int maxRows)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var categories =
        //            _categoryService.GetAll().ToList().OrderBy(
        //                c => CategoryNew.ResourceManager.GetString(c.Key, Thread.CurrentThread.CurrentCulture)).Select(
        //                    c => new
        //                             {
        //                                 Description =
        //                             CategoryNew.ResourceManager.GetString(c.Key, Thread.CurrentThread.CurrentCulture),
        //                                 c.Id
        //                             });
        //        if (searchTerm != "")
        //        {
        //            categories =
        //                categories.Where(l => l.Description.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
        //        }
        //        return Json(categories, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
    }
}

